// package com.example.demo;
//
// import org.springframework.context.ApplicationContext;
// import org.springframework.context.support.ClassPathXmlApplicationContext;
// import reactor.util.function.Tuple2;
//
// import java.io.FileNotFoundException;
// import java.io.FileOutputStream;
// import java.math.BigInteger;
//
// public class AssetClient {
//
//     static Logger logger = LoggerFactory.getLogger(AssetClient.class);
//
//     //sdk
//     private BcosSDK bcosSDK;
//
//     private Client client;
//     //加密密钥对
//     private CryptoKeyPair cryptoKeyPair;
//
//
//     /**
//      * 初始化
//      * @throws Exception
//      */
//     public void initialize() throws Exception {
//         @SuppressWarnings("resource")
//         ApplicationContext context =
//                 new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
//         bcosSDK = context.getBean(BcosSDK.class);
//         client = bcosSDK.getClient(1);
//         //创建key
//         cryptoKeyPair = client.getCryptoSuite().createKeyPair();
//         client.getCryptoSuite().setCryptoKeyPair(cryptoKeyPair);
//
//         logger.debug("create client for group1, account address is " + cryptoKeyPair.getAddress());
//     }
//
//     //部署资产和记录地址
//     public void deployAssetAndRecordAddr() {
//
//         try {
//             //部署
//             Asset asset = Asset.deploy(client, cryptoKeyPair);
//             System.out.println(
//                     " deploy Asset success, contract address is " + asset.getContractAddress());
//
//             //设置地址
//             recordAssetAddr(asset.getContractAddress());
//         } catch (Exception e) {
//             // TODO Auto-generated catch block  自动生成捕获块
//             // e.printStackTrace();
//             System.out.println(" deploy Asset contract failed, error message is  " + e.getMessage());
//         }
//     }
//
//     /**
//      * 记录
//      * @param address
//      * @throws FileNotFoundException
//      * @throws IOException
//      */
//     public void recordAssetAddr(String address) throws FileNotFoundException, IOException {
//         Properties prop = new Properties();
//
//         prop.setProperty("address", address);
//         final Resource contractResource = new ClassPathResource("contract.properties");
//         FileOutputStream fileOutputStream = new FileOutputStream(contractResource.getFile());
//         //写入
//         prop.store(fileOutputStream, "contract address");
//     }
//
//     /**
//      * 加载
//      * @return
//      * @throws Exception
//      */
//     public String loadAssetAddr() throws Exception {
//         // load Asset contact address from contract.properties
//         Properties prop = new Properties();
//
//         final Resource contractResource = new ClassPathResource("contract.properties");
//         prop.load(contractResource.getInputStream());
//
//         String contractAddress = prop.getProperty("address");
//         // .trim()删除空格
//         if (contractAddress == null || contractAddress.trim().equals("")) {
//             throw new Exception(" load Asset contract address failed, please deploy it first. ");
//         }
//         logger.info(" load Asset address from contract.properties, address is {}", contractAddress);
//         return contractAddress;
//     }
//
//     /**
//      * 查询账户
//      * @param assetAccount
//      */
//     public void queryAssetAmount(String assetAccount) {
//         try {
//             //加载地址
//             String contractAddress = loadAssetAddr();
//             //加载合约
//             Asset asset = Asset.load(contractAddress, client, cryptoKeyPair);
//             //查询账户
//             Tuple2<BigInteger, BigInteger> result = asset.select(assetAccount);
//             //账户存不存在
//             if (result.getValue1().compareTo(new BigInteger("0")) == 0) {
//                 System.out.printf(" asset account %s, value %s \n", assetAccount, result.getValue2());
//             } else {
//                 System.out.printf(" %s asset account is not exist \n", assetAccount);
//             }
//         } catch (Exception e) {
//             // TODO Auto-generated catch block
//             // e.printStackTrace();
//             logger.error(" queryAssetAmount exception, error message is {}", e.getMessage());
//
//             System.out.printf(" query asset account failed, error message is %s\n", e.getMessage());
//         }
//     }
//
//     /**
//      * 注册账户
//      * @param assetAccount
//      * @param amount
//      */
//     public void registerAssetAccount(String assetAccount, BigInteger amount) {
//         try {
//             String contractAddress = loadAssetAddr();
//
//             Asset asset = Asset.load(contractAddress, client, cryptoKeyPair);
//             TransactionReceipt receipt = asset.register(assetAccount, amount);
//             List<Asset.RegisterEventEventResponse> response = asset.getRegisterEventEvents(receipt);
//             if (!response.isEmpty()) {
//                 if (response.get(0).ret.compareTo(new BigInteger("0")) == 0) {
//                     System.out.printf(
//                             " register asset account success => asset: %s, value: %s \n", assetAccount, amount);
//                 } else {
//                     System.out.printf(
//                             " register asset account failed, ret code is %s \n", response.get(0).ret.toString());
//                 }
//             } else {
//                 System.out.println(" event log not found, maybe transaction not exec. ");
//             }
//         } catch (Exception e) {
//             // TODO Auto-generated catch block
//             // e.printStackTrace();
//
//             logger.error(" registerAssetAccount exception, error message is {}", e.getMessage());
//             System.out.printf(" register asset account failed, error message is %s\n", e.getMessage());
//         }
//     }
//
//     /**
//      * 转移资产
//      * @param fromAssetAccount
//      * @param toAssetAccount
//      * @param amount
//      */
//     public void transferAsset(String fromAssetAccount, String toAssetAccount, BigInteger amount) {
//         try {
//             String contractAddress = loadAssetAddr();
//             Asset asset = Asset.load(contractAddress, client, cryptoKeyPair);
//             TransactionReceipt receipt = asset.transfer(fromAssetAccount, toAssetAccount, amount);
//             List<Asset.TransferEventEventResponse> response = asset.getTransferEventEvents(receipt);
//             if (!response.isEmpty()) {
//                 if (response.get(0).ret.compareTo(new BigInteger("0")) == 0) {
//                     System.out.printf(
//                             " transfer success => from_asset: %s, to_asset: %s, amount: %s \n",
//                             fromAssetAccount, toAssetAccount, amount);
//                 } else {
//                     System.out.printf(
//                             " transfer asset account failed, ret code is %s \n", response.get(0).ret.toString());
//                 }
//             } else {
//                 System.out.println(" event log not found, maybe transaction not exec. ");
//             }
//         } catch (Exception e) {
//             // TODO Auto-generated catch block
//             // e.printStackTrace();
//
//             logger.error(" registerAssetAccount exception, error message is {}", e.getMessage());
//             System.out.printf(" register asset account failed, error message is %s\n", e.getMessage());
//         }
//     }
//
//     /**
//      * Usage 使用
//      */
//     public static void Usage() {
//         System.out.println(" Usage:");
//         System.out.println(
//                 "\t java -cp conf/:lib/*:apps/* org.fisco.bcos.asset.client.AssetClient deploy");
//         System.out.println(
//                 "\t java -cp conf/:lib/*:apps/* org.fisco.bcos.asset.client.AssetClient query account");
//         System.out.println(
//                 "\t java -cp conf/:lib/*:apps/* org.fisco.bcos.asset.client.AssetClient register account value");
//         System.out.println(
//                 "\t java -cp conf/:lib/*:apps/* org.fisco.bcos.asset.client.AssetClient transfer from_account to_account amount");
//         System.exit(0);
//     }
//
//     public static void main(String[] args) throws Exception {
//         if (args.length < 1) {
//             Usage();
//         }
//
//         AssetClient client = new AssetClient();
//         client.initialize();
//
//         switch (args[0]) {
//             case "deploy":
//                 client.deployAssetAndRecordAddr();
//                 break;
//             case "query":
//                 if (args.length < 2) {
//                     Usage();
//                 }
//                 client.queryAssetAmount(args[1]);
//                 break;
//             case "register":
//                 if (args.length < 3) {
//                     Usage();
//                 }
//                 client.registerAssetAccount(args[1], new BigInteger(args[2]));
//                 break;
//             case "transfer":
//                 if (args.length < 4) {
//                     Usage();
//                 }
//                 client.transferAsset(args[1], args[2], new BigInteger(args[3]));
//                 break;
//             default:
//             {
//                 Usage();
//             }
//         }
//         System.exit(0);
//     }
//
// }
